programming4us
           
 
 
Programming

Programming Excel with VBA and .NET : Tasks in Visual Basic - Interact with Users

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/18/2011 5:28:02 PM

1. Types of Tasks

You can perform several broad categories of tasks with the Visual Basic language. Since we're talking about Visual Basic (not Excel), these tasks tend to be very general. More specific tasks are usually handled through Excel objects.

Table 1. An overview of Visual Basic programming tasks
CategorySectionOverview
UsersInteract with UsersUse simple dialog boxes to get or display information.
DataDo MathPerform calculations.
 Work with TextCompose and modify strings of text.
 Get Dates and TimesGet current dates and times and perform calculations on dates and times.
StorageRead and Write FilesOpen, read, write, and close files stored on disk.
ExpressionsCheck ResultsFind what kind of data was returned by an operation.
 Find TruthCombine expressions to create complex conditions.
 Compare BitsGet multiple pieces of information from a single value.
InteroperateRun Other ApplicationsExchange data with other Windows applications.
CompilerControl the CompilerCreate debug and release versions of code within the same source file.

2. Interact with Users

Visual Basic provides two simple ways to interact with users:

  • Use InputBox to get text input.

  • Use MsgBox to display short messages and get button-click responses.

I've already used InputBox and MsgBox a number of times in previous examples, but one more sample won't hurt:

    Sub MsgBoxInputBox( )
Dim str As String, val As VbMsgBoxResult
' InputBox gets simple text input.
str = InputBox("Enter some text.", "Chapter 3", "Some text")
' Use If to test if a value was entered.
If str <> "" Then
' You can combine style constants in MsgBox.
val = MsgBox(str, vbQuestion + vbOKCancel, "Chapter 3")
' Return value indicates which button was clicked.
If val = vbOK Then Debug.Print "OK" Else Debug.Print "Cancel"
End If
End Sub

The preceding code displays a simple dialog box to get text, then displays the text in another simple dialog box, as shown in Figure 1.

The MsgBox function can display many different styles and buttons, depending on the Button argument setting. All of the VbMsgBoxStyle settings are listed in Table 2.

Figure 1. InputBox and MsgBox functions display simple dialog boxes

Table 2. VbMsgBoxStyle settings
SettingDescription
Button
vbOKOnlyDisplays OK button.
vbOKCancelDisplays OK, Cancel buttons.
vbAbortRetryIgnoreDisplays Abort, Retry, Ignore buttons.
vbYesNoCancelDisplays Yes, No, Cancel buttons.
vbYesNoDisplays Yes, No buttons.
vbRetryCancelDisplays Retry, Cancel buttons.
Icon
vbCriticalAdds Critical icon (red x).
vbQuestionAdds Question icon (?).
vbExclamationAdds Exclamation icon (!).
vbInformationAdds Information icon (i).
Default setting
vbDefaultButton1First button is default.
vbDefaultButton2Second button is default.
vbDefaultButton3Third button is default.
vbDefaultButton4Fourth button is default.
Focus
vbApplicationModalHalts workbook until dialog box is closed (this is the default in Excel).
vbSystemModalHalts all applications until dialog box is closed.
vbMsgBoxSetForegroundDisplays dialog in the foreground (this is the default in Excel).
Miscellaneous
vbMsgBoxHelpButtonAdds a Help button to the dialog.
vbMsgBoxRightRight-aligns text (default is left-aligned).
vbMsgBoxRtlReadingSwaps icon and button positions for right-to-left reading languages such as Arabic.

Compatible settings in Table 2 can be combined using addition. For instance, you can combine button, icon, and default settings in a single message box as shown here:

    val = MsgBox("Unable to continue.", _
vbCritical + vbAbortRetryIgnore + vbDefaultButton2, "Error")

The value returned by MsgBox is a VbMsgBoxResult constant that indicates which button the user clicked. Typically, you compare that result to the button constants listed in Table 2 in an If or Select statement:

    If val = VbMsgBoxResult.vbAbort Then ...

If you are displaying a dialog with only a single OK button, you probably don't care about the value returned by MsgBox. In that case, you can omit the parentheses:

    MsgBox "The answer is " & val, , "Chapter 3"

In addition to Visual Basic's built-in InputBox and MsgBox functions, there are several other ways to display much more complex dialog boxes and data-entry forms from Excel (see Table 3).

Table 3. Ways to display complex data-entry forms and dialog boxes
TechniqueUse to
User formsCreate custom dialog boxes or data-entry forms for display from Visual Basic
Excel's built-in dialogsDisplay the standard Excel dialog boxes to get filenames, printer settings, or other common tasks
InfoPath formsCollect data in XML format

Other -----------------
- Context and Interception : The .NET Context
- Context and Interception : .NET Component Services
- Optimizing for Vertical Search : Mobile, Video & Multimedia Search
- Programming WCF Services : Data Contracts - Collections (part 2) - The CollectionDataContract Attribute & Dictionaries
- Programming WCF Services : Data Contracts - Collections (part 1) - Concrete Collections & Custom Collections
- iPhone Programming : The Image Picker View Controller - Adding the Image Picker to the City Guide Application
- iPhone Programming : Other View Controllers - Modal View Controllers
- jQuery 1.3 : DOM Manipulation - Inserting new elements
- jQuery 1.3 : DOM Manipulation - Manipulating attributes
- DirectX 10 Game Programming : Adding the DirectX Libraries
- jQuery 1.3 : AJAX - Additional options
- jQuery 1.3 : AJAX and events & Security limitations
- jQuery 1.3 : AJAX - Keeping an eye on the request
- jQuery 1.3 : AJAX - Passing data to the server
- iPhone Programming : Other View Controllers - Tab Bar Applications
- iPhone Programming : Other View Controllers - Utility Applications
- iPhone Programming : Table-View-Based Applications - Adding a City View
- iPhone Programming : Table-View-Based Applications - Adding Navigation Controls to the Application
- iPad SDK : Popovers - Popover Preparations
- iPad SDK : Preparing Dudel for a New Tool (part 5) - Rendering Multiple Styles
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us